package controller
{
    import model.AnalyticProxy;
    import model.CurrencyRatesProxy;
    import model.DictionaryProxy;
    import model.LogProxy;
    import model.UserProxy;
    import model.service.CurrencyRateService;
    import model.vo.ConfigVO;
    import model.vo.UserVO;

    import mx.core.FlexGlobals;

    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.command.SimpleCommand;

    /**
     *  Prepare Model are register all proxies with required configuration.
     */
    public class PrepareModelCommand extends SimpleCommand
    {
        override public function execute(notification:INotification):void
        {
            super.execute(notification);

            var config:ConfigVO = notification.getBody() as ConfigVO;
            var userProxy:UserProxy = new UserProxy(config);
            var user:UserVO = userProxy.user;
            user.email = getParam("email");
            user.countryCode = getParam("country_code").toLowerCase();

            facade.registerProxy(userProxy);
            facade.registerProxy(new DictionaryProxy(config));
            facade.registerProxy(new LogProxy(config));
            facade.registerProxy(new AnalyticProxy(user.usedTags, user.usedCurrencies, config));
        }

        /**
         * Read parameters from flash vars
         * @param paramName
         * @return
         */
        private function getParam(paramName:String):String
        {
            var param:Object = FlexGlobals.topLevelApplication.parameters;
            if (paramName in param && param[paramName])
                return param[paramName];
            return "";
        }
    }
}
